//Ajax拦截器
function Sendajax(url, transmission, data, tokenValue) {
    var Data = '';
    // 拦截
    $.ajaxSetup({
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        complete: function (XMLHttpRequest, textStatus) {
            try {
                var res = JSON.parse(XMLHttpRequest.responseText);
                switch (res.code) {
                    case(501):
                        stop()
                        layer.confirm('用户尚未登录，请先登录', {
                            btn: ['确定']
                            , btn1: function (index, layero) {
                                window.location.href = '../pages/userLoginPage.html'
                                move()
                            },
                            title:'提示'
                        });
                        window.alert = function () {
                            return false;
                        }
                        console.log(res.message);
                        break;
                    case(201):
                        console.log(res.message);
                        break;
                    case(500):
                        console.log(res.message);
                        break;
                    case(520):
                        console.log(res.message);
                        break;
                    case(521):
                        console.log(res.message);
                        break;
                    case(460):
                        console.log(res.message);
                        break;
                }
            } catch (e) {
                // consoe.log('成功');
            }
        }
    });


    $.ajax({
        url: url,
        type: transmission,
        dataType: 'json',
        async: false,
        data: data,
        contentType: "application/x-www-form-urlencoded",
        cache: false,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('token', tokenValue)
        },
        success: function (result) {
            Data = result;
            if (Data.code) {
                console.log(Data);
            }
        },
        error: function (result) {
            Data = result;
            console.log('失败' + Data.code);
        }
    });
    return Data;
}

//管理页  Ajax拦截器
function SendFileajax(url, transmission, data, tokenValue) {
    var Data = '';
    // 拦截
    $.ajaxSetup({
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        complete: function (XMLHttpRequest, textStatus) {
            try {
                var res = JSON.parse(XMLHttpRequest.responseText);
                switch (res.code) {
                    case(501):
                        alert('用户尚未登录，请先登录')
                        window.location.href = '../pages/userLoginPage.html'
                        window.alert = function () {
                            return false;
                        }
                        console.log(res.message);
                        break;
                    case(201):
                        console.log(res.message);
                        break;
                    case(500):
                        console.log(res.message);
                        break;
                    case(520):
                        console.log(res.message);
                        break;
                    case(521):
                        console.log(res.message);
                        break;
                    case(460):
                        console.log(res.message);
                        break;
                }
            } catch (e) {
                // consoe.log('成功');
            }
        }
    });

    $.ajax({
        url: url,
        type: transmission,
        dataType: 'json',
        async: false,
        data: data,
        cache: false,
        processData: false,
        contentType: false,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('token', tokenValue)
        },
        success: function (result) {
            Data = result;
            if (Data.code) {
                console.log(Data);
            }
        },
        error: function (result) {
            Data = result;
            console.log('失败' + Data.code);
        }
    });
    return Data;
}


function SendFileajaxPc(url, transmission, data, tokenValue) {
    var Data = '';
    // 拦截
    $.ajaxSetup({
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        complete: function (XMLHttpRequest, textStatus) {
            try {
                var res = JSON.parse(XMLHttpRequest.responseText);
                switch (res.code) {
                    case(501):
                        alert('用户尚未登录，请先登录')
                        window.location.href = '../pages/pcLoginPage.html';
                        window.alert = function () {
                            return false;
                        };
                        console.log(res.message);
                        break;
                    case(201):
                        console.log(res.message);
                        break;
                    case(500):
                        console.log(res.message);
                        break;
                    case(520):
                        console.log(res.message);
                        break;
                    case(521):
                        console.log(res.message);
                        break;
                    case(460):
                        console.log(res.message);
                        break;
                }
            } catch (e) {
                // consoe.log('成功');
            }
        }
    });

    $.ajax({
        url: url,
        type: transmission,
        dataType: 'json',
        async: false,
        data: data,
        cache: false,
        processData: false,
        contentType: false,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('token', tokenValue)
        },
        success: function (result) {
            Data = result;
            if (Data.code) {
                console.log(Data);
            }
        },
        error: function (result) {
            Data = result;
            console.log('失败' + Data.code);
        }
    });
    return Data;
}

var mo = function (e) {
    e.preventDefault();
};

function stop() {
    document.body.style.overflow = 'hidden';
    document.addEventListener("touchmove", mo, false);//禁止页面滑动
}

/***取消滑动限制***/
function move() {
    document.body.style.overflow = '';//出现滚动条
    document.removeEventListener("touchmove", mo, false);
}

//侧边导航栏

$(".sidebar").on("click", function () {
    $(".sidebar-box").css("display", "block")
    setTimeout(function () {
        $(".sidebar-box").addClass("slide")
    }, 100)
    stop()
})

$("#nav").on("click", function (e) {
    e.stopPropagation()
    $(".sidebar-box").removeClass("slide")
    setTimeout(function () {
        $(".sidebar-box").css("display", "none")
    }, 300)
    move()
})

$(".back").on("click", function () {
    window.history.back(-1)
})


// 阻止双击放大
var lastTouchEnd = 0;
document.addEventListener('touchstart', function (event) {
    if (event.touches.length > 1) {
        event.preventDefault();
    }
});
document.addEventListener('touchend', function (event) {
    var now = (new Date()).getTime();
    if (now - lastTouchEnd <= 300) {
        event.preventDefault();
    }
    lastTouchEnd = now;
}, false);

// 阻止双指放大
document.addEventListener('gesturestart', function (event) {
    event.preventDefault();
});


//懒加载
var imgs = document.querySelectorAll('img');

function getTop(e) {
    return e.offsetTop;
}

function lazyload(imgs) {
    var h = window.innerHeight;
    var s = document.documentElement.scrollTop || document.body.scrollTop;
    for (var i = 0; i < imgs.length; i++) {
        if ((h + s) > getTop(imgs[i])) {
            (function (i) {
                setTimeout(function () {
                    var temp = new Image();
                    temp.src = imgs[i].getAttribute('data-src');
                    temp.onload = function () {
                        imgs[i].src = imgs[i].getAttribute('data-src')
                    }
                }, 2000)
            })(i)
        }
    }
}

lazyload(imgs);

window.onscroll = function () {
    lazyload(imgs);
}
window.ontouchmove = function () {
    lazyload(imgs);
}